﻿
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace EnterpriseFund.DAL
{
	/// <summary>
	/// 数据访问类:SupportPolicy
	/// </summary>
	public partial class SupportPolicy
	{
		public SupportPolicy()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string FPolicyID,decimal FYear)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from fund.t_SupportPolicy");
			strSql.Append(" where FPolicyID=@FPolicyID and FYear=@FYear");
			SqlParameter[] parameters = {
					new SqlParameter("@FPolicyID", SqlDbType.NVarChar,50),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),			};
			parameters[0].Value = FPolicyID;
			parameters[1].Value = FYear;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(EnterpriseFund.Model.SupportPolicy model)
		{
            var getBillIdSql = "exec sp_getbillno 'fund.t_SupportPolicy','FPolicyID','','','',@returnBId output ";
            var getBillIdPar = new SqlParameter[]{ 
                new SqlParameter("@returnBId", SqlDbType.VarChar, 50)
            };
            getBillIdPar[0].Direction = ParameterDirection.Output;


			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into fund.t_SupportPolicy(");
            strSql.Append("FPolicyID,FYear,FEnterprisePropNo,FTaxClassAllowanceNo,FRuleDesc,FRuleFormula,FRuleExpression,FIsEvalBase,FInnerRate,FOuterRate,FComputeDesc,FComputeFormula,FComputeExpression,FNote,FInsEmpNo,FInsDeptNo,FInsDate,FInsIP,FUpdEmplNo,FUpdDeptNo,FUpdDate,FUpdIP,FProp,FPolicyName)");
			strSql.Append(" values (");
            strSql.Append("@FPolicyID,@FYear,@FEnterprisePropNo,@FTaxClassAllowanceNo,@FRuleDesc,@FRuleFormula,@FRuleExpression,@FIsEvalBase,@FInnerRate,@FOuterRate,@FComputeDesc,@FComputeFormula,@FComputeExpression,@FNote,@FInsEmpNo,@FInsDeptNo,@FInsDate,@FInsIP,@FUpdEmplNo,@FUpdDeptNo,@FUpdDate,@FUpdIP,@FProp,@FPolicyName)");
			SqlParameter[] parameters = {
					new SqlParameter("@FPolicyID", SqlDbType.NVarChar,50),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FTaxClassAllowanceNo", SqlDbType.NVarChar,20),
					new SqlParameter("@FRuleDesc", SqlDbType.NVarChar,200),
					new SqlParameter("@FRuleFormula", SqlDbType.NVarChar,-1),
					new SqlParameter("@FRuleExpression", SqlDbType.NVarChar,-1),
					new SqlParameter("@FIsEvalBase", SqlDbType.NVarChar,1),
					new SqlParameter("@FInnerRate", SqlDbType.Decimal,5),
					new SqlParameter("@FOuterRate", SqlDbType.Decimal,5),
					new SqlParameter("@FComputeDesc", SqlDbType.NVarChar,200),
					new SqlParameter("@FComputeFormula", SqlDbType.NVarChar,-1),
					new SqlParameter("@FComputeExpression", SqlDbType.NVarChar,-1),
					new SqlParameter("@FNote", SqlDbType.NVarChar,-1),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime,30),
					new SqlParameter("@FUpdIP", SqlDbType.VarChar,15),
					new SqlParameter("@FProp", SqlDbType.NVarChar,1),
					new SqlParameter("@FPolicyName", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.FPolicyID;
            parameters[0].Direction = ParameterDirection.InputOutput;
			parameters[1].Value = model.FYear;
			parameters[2].Value = model.FEnterprisePropNo;
			parameters[3].Value = model.FTaxClassAllowanceNo;
			parameters[4].Value = model.FRuleDesc;
			parameters[5].Value = model.FRuleFormula;
			parameters[6].Value = model.FRuleExpression;
			parameters[7].Value = model.FIsEvalBase;
			parameters[8].Value = model.FInnerRate;
			parameters[9].Value = model.FOuterRate;
			parameters[10].Value = model.FComputeDesc;
			parameters[11].Value = model.FComputeFormula;
			parameters[12].Value = model.FComputeExpression;
			parameters[13].Value = model.FNote;
			parameters[14].Value = model.FInsEmpNo;
			parameters[15].Value = model.FInsDeptNo;
			parameters[16].Value = model.FInsDate;
			parameters[17].Value = model.FInsIP;
			parameters[18].Value = model.FUpdEmplNo;
			parameters[19].Value = model.FUpdDeptNo;
			parameters[20].Value = model.FUpdDate;
			parameters[21].Value = model.FUpdIP;
            parameters[22].Value = model.FProp;
            parameters[23].Value = model.FPolicyName;

            var sqlList = new List<CommandInfo>();
            sqlList.Add(new CommandInfo(getBillIdSql, getBillIdPar));
            sqlList.Add(new CommandInfo(strSql.ToString(), parameters));

			DbHelperSQL.ExecuteSqlTranWithString(sqlList);
            return true;
		}


		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(EnterpriseFund.Model.SupportPolicy model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update fund.t_SupportPolicy set ");
			strSql.Append("FEnterprisePropNo=@FEnterprisePropNo,");
			strSql.Append("FTaxClassAllowanceNo=@FTaxClassAllowanceNo,");
			strSql.Append("FRuleDesc=@FRuleDesc,");
			strSql.Append("FRuleFormula=@FRuleFormula,");
			strSql.Append("FRuleExpression=@FRuleExpression,");
			strSql.Append("FIsEvalBase=@FIsEvalBase,");
			strSql.Append("FInnerRate=@FInnerRate,");
			strSql.Append("FOuterRate=@FOuterRate,");
			strSql.Append("FComputeDesc=@FComputeDesc,");
			strSql.Append("FComputeFormula=@FComputeFormula,");
			strSql.Append("FComputeExpression=@FComputeExpression,");
			strSql.Append("FNote=@FNote,");
			strSql.Append("FInsEmpNo=@FInsEmpNo,");
			strSql.Append("FInsDeptNo=@FInsDeptNo,");
			strSql.Append("FInsDate=@FInsDate,");
			strSql.Append("FInsIP=@FInsIP,");
			strSql.Append("FUpdEmplNo=@FUpdEmplNo,");
			strSql.Append("FUpdDeptNo=@FUpdDeptNo,");
			strSql.Append("FUpdDate=@FUpdDate,");
			strSql.Append("FUpdIP=@FUpdIP,");
            strSql.Append("FProp=@FProp,");
            strSql.Append("FPolicyName=@FPolicyName");
			strSql.Append(" where FPolicyID=@FPolicyID and FYear=@FYear");
			SqlParameter[] parameters = {
					new SqlParameter("@FRuleDesc", SqlDbType.NVarChar,200),
					new SqlParameter("@FRuleFormula", SqlDbType.NVarChar,-1),
					new SqlParameter("@FRuleExpression", SqlDbType.NVarChar,-1),
					new SqlParameter("@FIsEvalBase", SqlDbType.NVarChar,1),
					new SqlParameter("@FInnerRate", SqlDbType.Decimal,5),
					new SqlParameter("@FOuterRate", SqlDbType.Decimal,5),
					new SqlParameter("@FComputeDesc", SqlDbType.NVarChar,200),
					new SqlParameter("@FComputeFormula", SqlDbType.NVarChar,-1),
					new SqlParameter("@FComputeExpression", SqlDbType.NVarChar,-1),
					new SqlParameter("@FNote", SqlDbType.NVarChar,-1),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime,30),
					new SqlParameter("@FUpdIP", SqlDbType.VarChar,15),
					new SqlParameter("@FPolicyID", SqlDbType.NVarChar,50),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FTaxClassAllowanceNo", SqlDbType.NVarChar,20),
					new SqlParameter("@FProp", SqlDbType.VarChar,1),
					new SqlParameter("@FPolicyName", SqlDbType.VarChar,50)
                                        };
			parameters[0].Value = model.FRuleDesc;
			parameters[1].Value = model.FRuleFormula;
			parameters[2].Value = model.FRuleExpression;
			parameters[3].Value = model.FIsEvalBase;
			parameters[4].Value = model.FInnerRate;
			parameters[5].Value = model.FOuterRate;
			parameters[6].Value = model.FComputeDesc;
			parameters[7].Value = model.FComputeFormula;
			parameters[8].Value = model.FComputeExpression;
			parameters[9].Value = model.FNote;
			parameters[10].Value = model.FInsEmpNo;
			parameters[11].Value = model.FInsDeptNo;
			parameters[12].Value = model.FInsDate;
			parameters[13].Value = model.FInsIP;
			parameters[14].Value = model.FUpdEmplNo;
			parameters[15].Value = model.FUpdDeptNo;
			parameters[16].Value = model.FUpdDate;
			parameters[17].Value = model.FUpdIP;
			parameters[18].Value = model.FPolicyID;
			parameters[19].Value = model.FYear;
			parameters[20].Value = model.FEnterprisePropNo;
			parameters[21].Value = model.FTaxClassAllowanceNo;
            parameters[22].Value = model.FProp;
            parameters[23].Value = model.FPolicyName;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string FPolicyID,decimal FYear)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from fund.t_SupportPolicy ");
			strSql.Append(" where FPolicyID=@FPolicyID and FYear=@FYear");
			SqlParameter[] parameters = {
					new SqlParameter("@FPolicyID", SqlDbType.NVarChar,50),
					new SqlParameter("@FYear", SqlDbType.Decimal,5)			};
			parameters[0].Value = FPolicyID;
			parameters[1].Value = FYear;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public EnterpriseFund.Model.SupportPolicy GetModel(string FPolicyID, decimal FYear)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 FPolicyID,FYear,FEnterprisePropNo,FTaxClassAllowanceNo,FRuleDesc,FRuleFormula,FRuleExpression,FIsEvalBase,FInnerRate,FOuterRate,FComputeDesc,FComputeFormula,FComputeExpression,FNote,FInsEmpNo,FInsDeptNo,FInsDate,FInsIP,FUpdEmplNo,FUpdDeptNo,FUpdDate,FUpdIP,FProp,FPolicyName from fund.t_SupportPolicy ");
            strSql.Append(" where  FPolicyID=@FPolicyID and FYear=@FYear");
			SqlParameter[] parameters = {
					new SqlParameter("@FPolicyID", SqlDbType.NVarChar,50),
					new SqlParameter("@FYear", SqlDbType.Decimal,5)
                                        };
            parameters[0].Value = FPolicyID;
            parameters[1].Value = FYear;

			EnterpriseFund.Model.SupportPolicy model=new EnterpriseFund.Model.SupportPolicy();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public EnterpriseFund.Model.SupportPolicy DataRowToModel(DataRow row)
		{
			EnterpriseFund.Model.SupportPolicy model=new EnterpriseFund.Model.SupportPolicy();
			if (row != null)
			{
				if(row["FPolicyID"]!=null)
				{
					model.FPolicyID=row["FPolicyID"].ToString();
				}
				if(row["FYear"]!=null && row["FYear"].ToString()!="")
				{
					model.FYear=decimal.Parse(row["FYear"].ToString());
				}
				if(row["FEnterprisePropNo"]!=null)
				{
					model.FEnterprisePropNo=row["FEnterprisePropNo"].ToString();
				}
				if(row["FTaxClassAllowanceNo"]!=null)
				{
					model.FTaxClassAllowanceNo=row["FTaxClassAllowanceNo"].ToString();
				}
				if(row["FRuleDesc"]!=null)
				{
					model.FRuleDesc=row["FRuleDesc"].ToString();
				}
				if(row["FRuleFormula"]!=null)
				{
					model.FRuleFormula=row["FRuleFormula"].ToString();
				}
				if(row["FRuleExpression"]!=null)
				{
					model.FRuleExpression=row["FRuleExpression"].ToString();
				}
				if(row["FIsEvalBase"]!=null)
				{
					model.FIsEvalBase=row["FIsEvalBase"].ToString();
				}
				if(row["FInnerRate"]!=null && row["FInnerRate"].ToString()!="")
				{
					model.FInnerRate=decimal.Parse(row["FInnerRate"].ToString());
				}
				if(row["FOuterRate"]!=null && row["FOuterRate"].ToString()!="")
				{
					model.FOuterRate=decimal.Parse(row["FOuterRate"].ToString());
				}
				if(row["FComputeDesc"]!=null)
				{
					model.FComputeDesc=row["FComputeDesc"].ToString();
				}
				if(row["FComputeFormula"]!=null)
				{
					model.FComputeFormula=row["FComputeFormula"].ToString();
				}
				if(row["FComputeExpression"]!=null)
				{
					model.FComputeExpression=row["FComputeExpression"].ToString();
				}
				if(row["FNote"]!=null)
				{
					model.FNote=row["FNote"].ToString();
				}
				if(row["FInsEmpNo"]!=null)
				{
					model.FInsEmpNo=row["FInsEmpNo"].ToString();
				}
				if(row["FInsDeptNo"]!=null)
				{
					model.FInsDeptNo=row["FInsDeptNo"].ToString();
				}
				if(row["FInsDate"]!=null && row["FInsDate"].ToString()!="")
				{
					model.FInsDate=DateTime.Parse(row["FInsDate"].ToString());
				}
				if(row["FInsIP"]!=null)
				{
					model.FInsIP=row["FInsIP"].ToString();
				}
				if(row["FUpdEmplNo"]!=null)
				{
					model.FUpdEmplNo=row["FUpdEmplNo"].ToString();
				}
				if(row["FUpdDeptNo"]!=null)
				{
					model.FUpdDeptNo=row["FUpdDeptNo"].ToString();
				}
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
				{
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
				}
				if(row["FUpdIP"]!=null)
				{
					model.FUpdIP=row["FUpdIP"].ToString();
				}
                if (row["FProp"] != null)
				{
                    model.FProp = row["FProp"].ToString();
				}
                if (row["FPolicyName"] != null)
				{
                    model.FPolicyName = row["FPolicyName"].ToString();
				}
                if (row.Table.Columns.Contains("FEnterprisePropName"))
                {
                    if (row["FEnterprisePropName"] != null)
                    {
                        model.FEnterprisePropName = row["FEnterprisePropName"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("FTaxClassAllowanceName"))
                {
                    if (row["FTaxClassAllowanceName"] != null)
                    {
                        model.FTaxClassAllowanceName = row["FTaxClassAllowanceName"].ToString();
                    }
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select sp.*,tax.FTaxClassAllowanceName,ent.FEnterprisePropName ");
			strSql.Append(" FROM fund.t_SupportPolicy AS sp ");
            strSql.Append(" INNER JOIN fund.t_EnterpriseProp AS ent ");
            strSql.Append(" ON sp.FEnterprisePropNo = ent.FEnterprisePropNo ");
            strSql.Append(" INNER JOIN fund.t_TaxClassAllowance AS tax ");
            strSql.Append(" ON sp.FTaxClassAllowanceNo = tax.FTaxClassAllowanceNo AND tax.FYear=sp.FYear ");
             
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" sp.* ");
            strSql.Append(" FROM fund.t_SupportPolicy AS sp ");
            strSql.Append(" INNER JOIN fund.t_EnterpriseProp AS ent ");
            strSql.Append(" ON sp.FEnterprisePropNo = ent.FEnterprisePropNo AND sp.FYear = ent.FYear ");
            strSql.Append(" INNER JOIN fund.t_TaxClassAllowance AS tax ");
            strSql.Append(" ON sp.FTaxClassAllowanceNo = tax.FTaxClassAllowanceNo AND sp.FYear = ent.FYear");
            
            if (strWhere.Trim() != "")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM fund.t_SupportPolicy ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.FTaxClassAllowanceNo desc");
			}
			strSql.Append(")AS Row, T.*  from fund.t_SupportPolicy T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "fund.t_SupportPolicy";
			parameters[1].Value = "FTaxClassAllowanceNo";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetInfor(string strWhere,string filedOrder = "")
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM v_PolicyInfor");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }            
            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }

		#endregion  ExtensionMethod
	}
}

